package com.thysdy.flowable.controller;

import com.thysdy.flowable.entity.*;
import com.thysdy.flowable.exception.MyException;
import com.thysdy.flowable.service.TaskService;
import com.thysdy.flowable.vo.ReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TaskController {
    @Autowired
    TaskService taskService;

    /**
     * 获取我的待办任务
     *
     * @param processQueryVo
     * @return
     */
    @PostMapping("/getMyTasks")
    public ReturnVo getMyTasks(@RequestBody ProcessInstanceQueryVo processQueryVo) {
        try {
            PageEntity tasks = taskService.getMyTasks(processQueryVo);
            return ReturnVo.success(tasks);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 获取我的已办任务
     *
     * @param processQueryVo
     * @return
     */
    @PostMapping("/getMyApplyedTasks")
    public ReturnVo getMyApplyedTasks(@RequestBody ProcessInstanceQueryVo processQueryVo) {
        try {
            PageEntity tasks = taskService.getMyApplyedTasks(processQueryVo);
            return ReturnVo.success(tasks);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 执行任务
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/doTask")
    public ReturnVo doTask(@RequestBody RequestVo requestVo) {
        try {
            taskService.doTask(requestVo);
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 执行下一任务
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/doNextTask")
    public ReturnVo doNextTask(@RequestBody RequestVo requestVo) {
        try {
            taskService.doNextTask(requestVo);
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 转办任务
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/complaintTask")
    public ReturnVo complaintTask(@RequestBody RequestVo requestVo) {
        try {
            taskService.complaintTask(requestVo);
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }


    /**
     * 添加审核人、审核组
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/addNextTaskAssign")
    public ReturnVo addNowTaskAssign(@RequestBody RequestVo requestVo) {
        try {
            taskService.addNextTaskAssign(requestVo);
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 获取可退回节点
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/getBackNode")
    public ReturnVo getBackNode(@RequestBody RequestVo requestVo) {
        try {
            List<FlowNodeVo> backNode = taskService.getBackNode(requestVo.getId());
            return ReturnVo.success(backNode);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 退回
     *
     * @param request
     * @return
     */
    @PostMapping("/backRoll")
    public ReturnVo backRoll(@RequestBody RequestVo request) {
        try {
            taskService.rollback(request);
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }


    /**
     * 查询流程全部任务节点
     *
     * @param request
     * @return
     */
    @PostMapping("/getProcessTasksByDefinitionKey")
    public ReturnVo getProcessTasks(@RequestBody RequestVo request) {
        try {
            List<UserTaskVo> tasks = taskService.getProcessTasksByDefinitionKey(request);
            return ReturnVo.success(tasks);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 查询当前正在运行节点信息
     */
    @PostMapping("getNowTaskInfo")
    public ReturnVo getNowTaskInfo(@RequestBody RequestVo requestVo) {
        try {
            UserTaskVo task = taskService.getNowTaskInfo(requestVo);
            return ReturnVo.success(task);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 查询当前正在运行任务基本信息
     */
    @PostMapping("getTaskBasicInfo")
    public ReturnVo getTaskBasicInfo(@RequestBody RequestVo requestVo) {
        return ReturnVo.success(taskService.getNowTaskBasicInfo(requestVo));
    }

    /**
     * 查询当前正在运行节点信息
     */
    @PostMapping("getNowTaskAssignee")
    public ReturnVo getNowTaskAssignee(@RequestBody RequestVo requestVo) {
        return ReturnVo.success(taskService.getNowTaskAssignee(requestVo));

    }

    @GetMapping("queryProcessByTaskName")
    public ReturnVo queryProcessByTaskName(@RequestParam("taskName") String taskName,
                                           @RequestParam("processKey") String processKey) {
        return ReturnVo.success(taskService.taskQueryByName(processKey, taskName));
    }


}
